// Label - Implement the functionality of Label control
// inherit: Control

// option ---
// all options same as Control 
// text: string, the text displayed in the link label

// api ---
// all APIs inherit from Control
// text(text): get/set the text of the link label

jControl.Label = jControl.create("Label");

jControl.Label.prototype.defaultOption = function() {
    this.option.text = "";

    this.option.containerStyle = null;
    this.option.hoverStyle = null;

    this.events = ["onClick", "onMouseEnter", "onMouseLeave"];

    this.option.recordEvents = { self: ["click"] };
}

jControl.Label.prototype.initialize = function() {
    this.setContainerStyle("jcLabel", this.option.containerStyle);

    // create an temp div, same style as the container except height setting, for getting actual text size
    // no width and height: auto sized
    // only width defined: auto determine the height
    // only height defined: auto determine the width 
    var tempDiv = jControl.append("body", "<div></div>", { position: "absolute" });
    jControl.setStyle(tempDiv, { "jcb jcLabel": this.option.containerStyle });

    if (this.option.width) jControl.css(tempDiv, { width: this.option.width });

    jControl.setDivText(tempDiv, this.option.text);
    this.textSize = { width: jControl.width(tempDiv), height: jControl.height(tempDiv) };
    $(tempDiv).remove();

    var width = this.option.width || jControl.getStyleFromCascade({ "jcb jcLabel": this.option.containerStyle }, "width");
    if (!width) this.css({ width: this.textSize.width + 2 }); // add 2 pixels to make sure single line text not be wrapped
    if (this.height() == 0) this.css({ height: this.textSize.height });

    this.api.text(this.option.text);
}

jControl.Label.prototype.getter = function(targetName, param) {
    if (!targetName) return null;

    switch (targetName.toLowerCase()) {
        case "text": return this.api.text();
    }
}

jControl.Label.prototype.setter = function(targetName, value) {
    if (!targetName) return;

    switch (targetName.toLowerCase()) {
        case "text": { this.api.text(value); break; }
    }
}

jControl.Label.prototype.setupApi = function() {
    var oThis = this;
    this.api = $.extend(this.api, {
        textDiv: function() { return oThis.innerDiv; },

        // methods ---
        text:  // get/set the label text
            function(text) {
                if (typeof text === "string") 
                    jControl.setDivText(oThis.container, text);
                else
                    return jControl.getDivText(oThis.container);
            },

        getTextSize: // ge the actual size of the text
             function() { return oThis.textSize }
    });
}

jControl.Label.prototype.bindEvent = function() {
    var oThis = this;

    this.bind("click", function(e) {
        oThis.fireEvent("onClick", e);
        oThis.record("click", { e: e });
    })

    this.bind("mouseenter", function(e) {
        oThis.fireEvent("onMouseEnter", e);
        oThis.record("mouseEnter", { e: e });
    });

    this.bind("mouseleave", function(e) {
        oThis.fireEvent("onMouseLeave", e);
        oThis.record("mouseLeave", { e: e });
    });
}

jControl.Label.prototype.test = function(action) {
    action = action.toLowerCase();
    if (action == "mouseenter")
        this.fireEvent("onMouseEnter");
    else if (action == "mouseleave")
        this.fireEvent("onMouseLeave");
    else if (action == "click")
        this.fireEvent("onClick");
}